export function hashCode(str) {
    let hash = 0;
    for (let i = 0; i < str.length; i++) {
        var character = str.charCodeAt(i);
        hash = ((hash << 5) - hash) + character;
        hash = hash & hash;
    }
    return hash;
}

export function injectScript(path) {
    let src = chrome.runtime.getURL(path);
    let id = 'script_' + hashCode(path);
    return new Promise((resolve, reject) => {
        if (!document.querySelector(`#${id}`)) {
            let script = document.createElement('script');
            script.setAttribute('id', id);
            script.setAttribute('src', src);
            script.onload = () => resolve();
            script.onerror = e => reject(e);
            document.head.appendChild(script);
        } else {
            resolve();
        }
    });
}